<style scoped>
    .layui-form >>> .layui-form-item{
        z-index: 10000;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>修改组织</legend>
        </fieldset>

        <form class="layui-form " >
            <input type="hidden" name="guid" placeholder="" autocomplete="off" :value="guid" class="layui-input">
            <div class="layui-form-item">
                <label class="layui-form-label">组织名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" :value="adata.name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">经营范围</label>
                <div class="layui-input-block" id="categoryids">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">公司类型  </label>
                <div class="layui-input-inline">
                    <select name="type" id="type" lay-verify="required"  v-model="adata.type">
                        <option value="1" >采收主体</option>
                        <option value="2" >普通</option>
                    </select>
                </div>
            </div>
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">组织类型</label>-->
<!--                <div class="layui-input-inline">-->
<!--                    <select name="type" id="type" lay-verify="required">-->
<!--                        <option value="1">销售公司</option>-->
<!--                        <option value="2">土豆集</option>-->
<!--                        <option value="3">其他</option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->
            <input type="hidden" name="type" placeholder="" autocomplete="off" value="1" class="layui-input">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="remark" :value="adata.remark" placeholder="请输入备注内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'EditOrganization',
        props: ['id'],
        data () {
            return {
                 guid : this.id,
                 adata: {},
                categoryids: [],
                currCates: []
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                let loader = layer.load();
                //获取回显数据
                $.ajax({
                    url: '/sales/Organization!get.action',
                    data: {guid: that.guid},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != null) {
                            that.adata = res.info;
                            that.getCategorys();
                            //分类回显
                            that.currCates = that.adata.categoryids.split(',');
                            $('#type').val(res.info.type);
                            form.render('select');
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/sales/Organization!update.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "ok") {
                                that.toast("修改成功");
                                that.$parent.switchPage('Organization');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });
        },
        methods: {
            getCategorys: function () {
                this.$post('/sales/Category!getAll.action?')
                    .then(response => {
                        this.categoryids = response.data.info;
                        //状态多选
                        this.initCategorysSelect();
                    });
            },
            initCategorysSelect: function () {
                let cate = layui.xmSelect.render({
                    el: '#categoryids',
                    tips: '请选择分类',
                    name: 'categoryids',
                    prop: {
                        name: 'name',
                        value: 'guid'
                    },
                    data: this.categoryids
                });
                cate.setValue(this.currCates);
            },
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            }
        }
    }
</script>
